package com.xm;

import java.io.*;
import java.net.URISyntaxException;
import java.time.LocalDateTime;

public class UnzipAndDelete {
    private static final String LOG_FILE;

   static  {
       String jarPath;
       try {
           jarPath = UnzipAndDelete.class.getProtectionDomain()
                   .getCodeSource().getLocation().toURI().getPath();
       } catch (URISyntaxException e) {
           throw new RuntimeException(e);
       }
       String jarDir = new File(jarPath).getParent();
       LOG_FILE = jarDir + File.separator + "UnzipAndDelete.log";
    }

    public UnzipAndDelete() {
    }

    public static void main(String[] var0) {
        try {
            log("程序启动，参数: " + String.join(" ", var0));
            execute(var0);
            log("程序正常完成");
            System.exit(0);
        } catch (Exception var2) {
            logError("程序异常: " + var2.getMessage());
            var2.printStackTrace();
            waitForEnter();
        }

    }

    private static void execute(String[] var0) throws Exception {
        if (var0.length == 0) {
            logError("错误: 未提供文件参数");
            throw new IllegalArgumentException("请拖放文件到本程序上使用");
        } else {
            String var1 = var0[0];
            File var2 = new File(var1);
            if (!var2.exists()) {
                logError("错误: 文件不存在 - " + var1);
                throw new FileNotFoundException("文件不存在: " + var1);
            } else {
                String var3 = findWinRARPath();
                if (var3 == null) {
                    logError("错误: 未找到WinRAR安装路径");
                    throw new IllegalStateException("未找到WinRAR安装路径");
                } else {
                    log("开始解压: " + var1);
                    String var4 = var2.getParent();
                    Process var5 = (new ProcessBuilder(new String[0])).command(var3, "x", var1, var4, "-y").inheritIO().start();
                    int var6 = var5.waitFor();
                    log("WinRAR解压完成，退出码: " + var6);
                    if (var6 != 0) {
                        logError("解压失败，错误代码: " + var6);
                        throw new RuntimeException("WinRAR解压失败");
                    } else if (!var2.delete()) {
                        logError("警告: 无法删除源文件 - " + var1);
                        throw new IOException("删除源文件失败");
                    } else {
                        log("操作成功完成，已删除源文件");
                    }
                }
            }
        }
    }

    private static String findWinRARPath() {
        String[] path = new String[]{"C:\\Program Files\\WinRAR\\WinRAR.exe", "C:\\Program Files (x86)\\WinRAR\\WinRAR.exe"};
        for(int var3 = 0; var3 < path.length; ++var3) {
            String var4 = path[var3];
            if ((new File(var4)).exists()) {
                log("找到WinRAR路径: " + var4);
                return var4;
            }
        }
        return null;
    }

    private static void waitForEnter() {
        System.err.println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
        System.err.println("程序执行遇到错误，请查看上方错误信息");
        System.err.println("按Enter键退出...");
        System.err.println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");

        try {
            System.in.read();
        } catch (IOException var1) {
            var1.printStackTrace();
        }

    }

    private static void log(String var0) {
        try {
            PrintWriter var1 = new PrintWriter(new FileWriter(LOG_FILE, true));

            try {
                LocalDateTime localDateTime = LocalDateTime.now();
                var1.println(localDateTime + " [INFO] " + var0);
            } catch (Throwable var5) {
                try {
                    var1.close();
                } catch (Throwable var4) {
                    var5.addSuppressed(var4);
                }

                throw var5;
            }

            var1.close();
        } catch (IOException var6) {
            System.err.println("无法写入日志: " + var6.getMessage());
        }

        System.out.println(var0);
    }

    private static void logError(String message) {
        try {
            PrintWriter var1 = new PrintWriter(new FileWriter(LOG_FILE, true));

            try {
                LocalDateTime dateTime = LocalDateTime.now();
                var1.println(dateTime + " [ERROR] " + message);
            } catch (Throwable e) {
                try {
                    var1.close();
                } catch (Throwable var4) {
                    e.addSuppressed(var4);
                }
                throw e;
            }

            var1.close();
        } catch (IOException var6) {
            System.err.println("无法写入错误日志: " + var6.getMessage());
        }

        System.err.println(message);
    }
}